package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车API")
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    CartService cartService;

    //获取购物车列表
    // http://api.gmall.com/api/cart/cartList
    @ApiOperation("获取购物车列表")
    @GetMapping("/cartList")
    public Result getCartList (){
//        //确定是哪个购物车
//        String cartKey = cartService.determinCartKey();
//        //查询购物车所有商品
//        List<CartInfo> cartInfos = cartService.getCartItems(cartKey);
        //合并购物车
        List<CartInfo> cartInfos = cartService.displayItems();
        return Result.ok(cartInfos);
    }
    //如果修改数量等 1 怎么办？ 前端做判断
    //为什么要先判断cartKey？
    // http://api.gmall.com/api/cart/addToCart/49/1
    @ApiOperation("修改购物车数量") // 1 -1 还是一个增量
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result updateItemNum (@PathVariable("skuId") Long skuId,@PathVariable("skuNum") Integer skuNum){
        String cartKey = cartService.determinCartKey();
        cartService.updateItemNum(skuId,skuNum,cartKey);
        return Result.ok();
    }
    // http://api.gmall.com/api/cart/checkCart/49/1
    @ApiOperation("选中商品")
    @GetMapping("/checkCart/{skuId}/{checked}")
    public Result checkItem (@PathVariable("skuId") Long skuId,@PathVariable("checked") Integer checked){
        String cartKey = cartService.determinCartKey();
        cartService.checkItem(skuId,checked,cartKey);
        return Result.ok();
    }
    // http://api.gmall.com/api/cart/deleteCart/49
    @ApiOperation("删除商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart (@PathVariable("skuId") Long skuId){
        String cartKey = cartService.determinCartKey();
        cartService.deleteCart(cartKey,skuId);
        return Result.ok();
    }
}
